package com.paradogs.framework.core.utils;

import com.paradogs.framework.core.annoations.rpc.PRRPCResponseResolver;
import com.paradogs.framework.core.msg.PRMsg;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/**
 * @author: yumi
 * @date: 2023/7/26  17:34
 * @Description: TODO
 */
@Component
public class EventUtils implements ApplicationEventPublisherAware {



    /**
     * Spring 事件发布机制
     */
    private static ApplicationEventPublisher applicationEventPublisher;

    /**
     * 发布事件
     * ps: 订阅事件由 Spring 的 @EventListener 注册
     * @param event
     */
    public static void publishEvent(ApplicationEvent event) {
        applicationEventPublisher.publishEvent(event);
    }

    /**
     * 监听一次
     * @param route
     * @param cgMsgId
     * @param returnClass
     * @param promise
     * @param <T>
     */
    public static <T> void on4Once(String route, Long cgMsgId, Class<?> returnClass, CompletableFuture<T> promise) {
        SpringUtils.getBean(PRRPCResponseResolver.class).on4Once(route, cgMsgId, returnClass, promise);
    }


    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        EventUtils.applicationEventPublisher = applicationEventPublisher;
    }

}
